﻿using System.Collections;
using System.Collections.Generic;
using GDGeek;
using MrPP.Mark;
using UnityEngine;

namespace Shows_3D
{
    public class Control : Singleton<Control>
    {
        private readonly FSM _fsm = new FSM();

        [SerializeField] private View view;

        // Start is called before the first frame update
        private void Start()
        {
            _fsm.addState("login",OnLogin());
            _fsm.addState("startStandAlone", OnStartStandAlone()); //单机模式
            _fsm.addState("host", OnHost());
            _fsm.addState("client", OnClient());
            // _fsm.addState("clientConnected", OnClientConnected()); //服务器,客户端链接成功
            // _fsm.addState("connectToServer", OnConnectToServer()); //客户端,链接服务器成功
            _fsm.addState("markFound", OnMark()); //通过二维码进行坐标系同步

            _fsm.init("login"); // 程序开始,默认进入单机模式
            //_fsm.init("startStandAlone");
        }

        private StateBase OnLogin()
        {
            State state = new State();
            state.addAction("startStandAlone", delegate(FSMEvent evt)
            {
                view.PostToFsm("startStandAlone");
            });
            return state;
        }

        private StateBase OnStartStandAlone()
        {
            State state = new State();
            state.addAction("startHost", evt => "host");
            state.addAction("startClient", evt => "client");
            return state;
        }

        private StateBase OnHost()
        {
            State state = new State();
            state.onStart += delegate { };
            state.addAction("onMarkFound","markFound");
            return state;
        }

        private StateBase OnClient()
        {
            State state = new State();
            state.addAction("onMarkFound","markFound");
            return state;
        }

        private StateBase OnMark()
        {
            State state = new State();
            state.onStart += delegate {  };
            state.onOver += delegate {  };
            
            return state;
        }

        public void PostToFsm(string evt)
        {
            _fsm.post(evt);
        }
    }
}